<?php
// +----------------------------------------------------------------------
// | 商品促销
// +----------------------------------------------------------------------
namespace app\admin\controller;
use think\facade\View;
use think\facade\Db;
class Cuxiao extends Base
{
    //列表
    public function index(){
        if(request()->isAjax()){
            $page = input('param.page');
            $limit = input('param.limit');
            if(input('param.field') && input('param.order')){
                $order = input('param.field').' '.input('param.order');
            }else{
                $order = 'sort desc,id desc';
            }
            $where = array();
            if($this->sid>0){
                $where[] = ['sid','=',$this->sid];
            }

            if(input('param.name')) $where[] = ['name','like','%'.input('param.name').'%'];
            $count = 0 + Db::name('cuxiao')->where($where)->count();
            $data = Db::name('cuxiao')->where($where)->page($page,$limit)->order($order)->select()->toArray();
            foreach($data as $k=>$v){
                if($v['starttime'] > time()){
                    $data[$k]['status'] = '未开始';
                }elseif($v['endtime'] < time()){
                    $data[$k]['status'] = '已结束';
                }else{
                    $data[$k]['status'] = '进行中';
                }
                $data[$k]['starttime'] = date('Y-m-d H:i',$v['starttime']);
                $data[$k]['endtime'] = date('Y-m-d H:i',$v['endtime']);
            }
            return json(['code'=>0,'msg'=>'查询成功','count'=>$count,'data'=>$data]);
        }
        return View::fetch();
    }
    //编辑
    public function edit(){
        if(input('param.id')){
            $where = [];
            $where[] = ['id','=',input('param.id/d')];
            if($this->sid>0){
                $where[] = ['sid','=',$this->sid];
            }
            $info = Db::name('cuxiao')->where($where)->find();
            $info['starttime'] = date('Y-m-d H:i:s',$info['starttime']);
            $info['endtime'] = date('Y-m-d H:i:s',$info['endtime']);
        }else{
            $info = array('id'=>'','starttime'=>date('Y-m-d 00:00:00'),'endtime'=>date('Y-m-d 00:00:00',time()+7*86400),'gettj'=>'-1','sort'=>0,'fwtype'=>0,'type'=>1,'tip'=>'满减');
        }
        $info['gettj'] = explode(',',$info['gettj']);
        $info['prozk'] = explode(',',$info['prozk']);
        $info['pronum'] = explode(',',$info['pronum']);
        if($info['proid']){
            $proinfo = Db::name('goods')->where('sid',$this->sid)->where('id',$info['proid'])->find();
            $gginfo = Db::name('spec_goods_price')->where('id',$info['ggid'])->find();
            View::assign('proinfo',$proinfo);
            View::assign('gginfo',$gginfo);
        }
        View::assign('info',$info);
        $memberlevel = Db::name('user_level')->order('rank,level_id')->select()->toArray();
        View::assign('memberlevel',$memberlevel);

        $categorydata = array();
        if($info && $info['categoryids']){
            $categorydata = Db::name('goods_category')->where('id','in',$info['categoryids'])->order('sort desc,id')->select()->toArray();
        }
        View::assign('categorydata',$categorydata);
        $productdata = array();
        if($info && $info['productids']){
            $productdata = Db::name('goods')->where('goods_id','in',$info['productids'])->order(Db::raw('field(id,'.$info['productids'].')'))->select()->toArray();
        }
        View::assign('productdata',$productdata);
        return View::fetch();
    }
    //保存
    public function save(){
        $info = input('post.info/a');
        $info['gettj'] = implode(',',$info['gettj']);
        $info['starttime'] = strtotime($info['starttime']);
        $info['endtime'] = strtotime($info['endtime']);
        $info['prozk'] = implode(',',$info['prozk']);
        $info['pronum'] = implode(',',$info['pronum']);

        $isonly = $this->isOnly($info);
        if($isonly['status'] == 0) {
            return json($isonly);
        }
        if($info['type']==1){
            //满额立减
            $info['minprice'] = input('minprice1')?:0;
            $info['money'] = input('money')?:0;
        }elseif($info['type']==2){
            //满额赠送
            $info['minprice'] = input('minprice2')?:0;
            $info['proid'] = input('proid2')?:'';
            $info['ggid'] = input('ggid2')?:'';
        }elseif($info['type']==3){
            //加价换购
            $info['money'] = input('money3')?:0;
            $info['proid'] = input('proid2')?:'';
            $info['ggid'] = input('ggid2')?:'';
        }elseif($info['type']==4){
            //满额打折
            $info['minprice'] = input('minprice4')?:0;
            $info['zhekou'] = input('zhekou4')?:'0';
        }elseif($info['type']==5){
            //满件打折
            $info['minnum'] = input('minnum5')?:0;
            $info['zhekou'] = input('zhekou5')?:'';
        }elseif($info['type']==6){
            //满件立减
            $info['minnum'] = input('minnum6')?:0;
            $info['money'] = input('money6')?:'';
        }
        if($info['id']){
            Db::name('cuxiao')->where('sid',$this->sid)->where('id',$info['id'])->update($info);
        }else{
            $info['sid'] = $this->sid;
            $info['createtime'] = time();
            $id = Db::name('cuxiao')->insertGetId($info);
        }
        return json(['status'=>1,'msg'=>'操作成功','url'=>(string)url('index')]);
    }
    public function isOnly($info)
    {
        return ['status'=>1,'msg'=>''];
    }
    //删除
    public function del(){
        $ids = input('post.ids/a');
        Db::name('cuxiao')->where('sid',$this->sid)->where('id','in',$ids)->delete();
        return json(['status'=>1,'msg'=>'删除成功']);
    }
}
